$(function () {
    $("#userId,#userPwd").focusin(function () {
        $(".msgbox").html('');
    })

    $("form").submit(function (e) {
        e.preventDefault();

        login();
    });

    if (window.parent != window.self) {
        window.top.location.reload();
    }
});

$(document).keydown(function (e) {
    var keycode = e.keyCode;
    if (keycode == 13) {
        login();
    }
});

function login() {
    $(".msgbox").html('正在登录，请稍等......').removeClass('error');
    $(".btn-login").prop("disabled", "disabled");

    var data = $('form').serializej();

    Base.ajax({
        "url": "/login/in",
        "data": data,
        "loading": false,
        "success": function (data) {
            if (data.code) {
                window.location.href = "/gis/index";
            } else {
                $(".msgbox").html('<span class="glyphicon glyphicon-exclamation-sign"></span> ' + data.msg).addClass("error");
            }

            $(".btn-login").removeAttr('disabled');
        },
        "error": function (jqXHR, status, errorThrown) {
            if (status == 'timeout') {
                $(".msgbox").html('登录请求超时，请重新提交！').addClass('error');
                $(".btn-login").removeAttr('disabled');
            }
        }
    });
}

